package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.enums.CommentType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 标注查询请求参数
 * @Date: 9:59 2019/9/18
 */

@Data
public class FindTagRequest {
    @ApiModelProperty(value = "案件ID")
    @NotNull(message = "personalId.is.required")
    private String caseId;

    @ApiModelProperty(value = "类型")
    private String type;

    @ApiModelProperty(value = "最小标注时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minOperateTime;

    @ApiModelProperty(value = "最大标注时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxOperateTime;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("caseId.keyword", this.caseId));
        if (StringUtils.isNotBlank(this.type)) {
            CommentType type;
            switch (this.type) {
                case "COMMENT":
                    type = CommentType.COMMENT;
                    break;
                case "POSTIL":
                    type = CommentType.POSTIL;
                    break;
                case "REMARKS":
                    type = CommentType.REMARKS;
                    break;
                case "REPORT_POSTIL":
                    type = CommentType.REPORT_POSTIL;
                    break;
                default:
                    type = CommentType.COMMENT;
                    break;
            }
            builder.must(QueryBuilders.matchPhraseQuery("commentType.keyword", type));
        }
        if (Objects.nonNull(this.minOperateTime)) {
            builder.must(QueryBuilders.rangeQuery("operatorTime").gte(this.minOperateTime.getTime()));
        }
        if (Objects.nonNull(this.maxOperateTime)) {
            builder.must(QueryBuilders.rangeQuery("operatorTime").lte(this.maxOperateTime.getTime() + 86400000));
        }
        return builder;
    }
}
